#ifndef __BS_IMAGE_MGR_H__
#define __BS_IMAGE_MGR_H__
#include <stdint.h>
#include "bs_app.h"



#pragma pack(push,1)

typedef struct bs_timespec {
    uint16_t        tm_year;
    uint8_t         tm_mon;
    uint8_t         tm_day;
    uint8_t         tm_hour;
    uint8_t         tm_min;
    uint8_t         tm_sec;
    uint16_t        tm_msec;
} bs_timespec_t;


typedef struct jouranl_info {
    uint32_t        log_addr;
    uint32_t        log_page_size;
    uint32_t        log_ofs;

    uint16_t        magic;
    uint8_t         type;
    uint8_t         b_len;
    uint8_t         b_data[24];
    uint32_t        check_sum;
} journal_t;


typedef struct bs_image_node {
    char            file_name[128+1];
    uint8_t         sfn[12];               /* 索引，用来匹配 fs 的目录项 */
    uint16_t        uid;
    bs_timespec_t   download_time;
    uint16_t        download_key;
    uint32_t        encrypted_file_len;
    uint32_t        encrypted_file_crc32;
    uint32_t        limit;
    uint32_t        inv_limit;
    uint32_t        used_count;
    uint32_t        inv_used_count;
    uint32_t        ok_count;
    uint32_t        inv_ok_count;
} bs_image_node_t;


typedef struct bs_images_mgr
{
    uint32_t         load_addr;
    uint32_t         bkp_addr;
    uint32_t         flag;
    uint8_t          i_active;
    uint8_t          *raw_data;

    // 下面的的数据是持久化存储
    uint32_t         len;
    uint32_t         crc32;
    uint16_t         images_num;
    char             activated_image[128+1];
    bs_image_node_t  images[MAX_IMAGE_FILES_NR];
    uint8_t          dummy[1];

} bs_images_mgr_t;


#pragma pack(pop)


int bs_imgr_init(void);
int bs_imgr_region_clear(void);
int bs_imgr_add_image(bs_image_node_t *new_img, FIL *fp);
int bs_imgr_save_image(FIL *fp);
int bs_imgr_del_image(uint8_t);

void bs_imgr_gettime(bs_timespec_t *ctim);
int bs_imgr_open_image(FIL *fp);
int bs_imgr_close_image(FIL *fp);
int bs_imgr_list_image(void);
int bs_imgr_select_image(uint8_t index);
#endif
